package com.whl.sm.controller.parking;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.parking.BlackWhiteParkingList;
import com.whl.sm.model.parking.ParkingLots;
import com.whl.sm.service.parking.BlackWhiteParkingListService;
import com.whl.sm.service.parking.ParkingLotsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/black-white-parking-list")
public class BlackWhiteParkingListController extends BaseController {

    @Autowired
    private BlackWhiteParkingListService blackWhiteParkingListService;

    @Autowired
    private ParkingLotsService parkingLotsService;

    @RequestMapping("/list")
    public Result getBlackAndWhiteList(HttpServletRequest request){
        QueryWrapper queryWrapper = getLikeParam("license_plate");
        setHttpRequestQueryWrapper(queryWrapper,"visible","parking_lots_id","list_type");
        queryWrapper.ne("is_del",1);
        Page<Object> listPage = blackWhiteParkingListService.page(getPage(),queryWrapper);
        listPage.getRecords().stream().forEach(obj->{
            BlackWhiteParkingList blackWhiteObj = (BlackWhiteParkingList) obj;
            ParkingLots parkingLots = parkingLotsService.getById(blackWhiteObj.getParkingLotsId());
            if (!Objects.isNull(parkingLots)){
                blackWhiteObj.setParkingCode(parkingLots.getParkingCode());
                blackWhiteObj.setParkingName(parkingLots.getParkingName());
            }
        });
        return Result.ok(listPage);
    }


    @RequestMapping("/info/{id}")
    public Result parkingInfo(@PathVariable("id") String id){
        BlackWhiteParkingList blackWhiteInfo = blackWhiteParkingListService.getById(id);
        return Result.ok(blackWhiteInfo);
    }

    @RequestMapping("/update")
    public Result updateParking (@RequestBody BlackWhiteParkingList blackWhiteParkingList){
        return blackWhiteParkingListService.update(blackWhiteParkingList);
    }


    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            blackWhiteParkingListService.update(new UpdateWrapper<BlackWhiteParkingList>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }
}
